AVRO-4324: [Java] Align ReflectDatumReader.readArray with GenericDatumReader eager-allocation guards - #3920
Open
iemejia wants to merge 1 commit into
Open
Conversation
ReflectDatumReader.readArray allocated the backing Java array for the declared array block count (Array.newInstance) before reading any element, unlike GenericDatumReader.readArray which already validates the count against the bytes remaining and caps element types whose minimum encoded size is zero. Apply the same guards (ensureAvailableCollectionBytes plus checkMaxCollectionAllocation for zero-byte element types) before the eager allocation, so a malformed or truncated record mapped to a Java array field (e.g. long[]) fails fast with an EOFException instead of over-allocating. Valid arrays continue to read unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
GenericDatumReader.readArrayalready validates a declared array block count against the bytes actually remaining (ensureAvailableCollectionBytes) and applies a heap-aware bound for element types whose minimum encoded size is zero, before eagerly allocating the backing storage.The
ReflectDatumReader.readArrayoverride did not apply these guards: it callednewArray(old, (int) l, ...)→Array.newInstance(elementClass, count)using the declared block count directly, so a malformed or truncated record mapped to a Java array field (for examplelong[]) could drive a large eager allocation before a single element was read.This aligns
ReflectDatumReader.readArraywith the generic reader by applying the sameensureAvailableCollectionBytesandcheckMaxCollectionAllocationguards before the allocation. Malformed input now fails fast (EOFException) instead of over-allocating; valid arrays read unchanged.How was this patch tested?
TestReflectDatumReader:read_PojoWithArray_rejectsOversizedArrayCount— a record declaring a ~2e9 array block count with no elements now fails fast instead of attempting a large allocation. Existing round-trip tests act as negative controls.avromodule test suite passes.JIRA